package nmp.remote;

import nmp.lib.CNmpDevice;
import nmp.lib.CNmpDongle;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class NmpStartup extends Activity  {
	
	private CNmpDongle mNmpDevice = null;
	private ImageView mImagePhone;
	private ImageView mImageDevice;
	private ImageView mImageNext;
	
	private TextView mTextPhoneName;
	private TextView mTextDeviceName;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
    	Log.d(this.toString(),"onCreate start");
    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.startup);      

		mImagePhone = (ImageView) findViewById(R.id.imagePhone);
		mImageDevice = (ImageView) findViewById(R.id.imageDevice);
		mImageNext = (ImageView) findViewById(R.id.imageNext);
		mTextPhoneName = (TextView) findViewById(R.id.textViewPhoneName);
		mTextDeviceName = (TextView) findViewById(R.id.textViewDeviceName);
		
		mTextPhoneName.setText(android.os.Build.MODEL);
		
        //mImageNext.setEnabled(false);
        //mImageNext.setVisibility(View.INVISIBLE);
        mImageNext.setClickable(true);
        mImagePhone.setClickable(true);
        mImageDevice.setClickable(true);

        mImagePhone.setOnClickListener(new OnClickListener() {
	            public void onClick(View v) {
					Intent intent = new Intent();
					//intent.setClass(NmpStartup.this, NmpTest.class);
					intent.setClass(NmpStartup.this, NmpDeviceStatus.class);
					startActivity(intent);	            	
	            }
    		} );
        
        mImageDevice.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	Toast.makeText(v.getContext(),
                        "Not implement yet!!",
                        Toast.LENGTH_LONG).show();
            	//switch to Connect
				//Intent intent = new Intent();
				//intent.setClass(NmpStartup.this, NmpTest.class);
				//startActivity(intent);	            	
            }
		} );
        
		Log.d(this.toString(),"onCreate end");
    }
    
    
    private void GotoMainPage() {
		NmpDeviceClose();		
		Intent intent = new Intent();
		intent.setClass(NmpStartup.this, NmpDeviceStatus.class);
		startActivity(intent);  	
    }
    
    private void ReConnect() {
    	NmpDeviceOpen();
    }
   
    
    private void ConnectResultHandle(boolean connect) {
    	if(connect){    	
    		
    		mImageDevice.setImageDrawable(getResources().getDrawable(R.drawable.device));    
    		mTextDeviceName.setText(mNmpDevice.GetCurrentWifiSSid());    		
            mImageNext.setImageDrawable(getResources().getDrawable(R.drawable.finger));  
            
    		mImageNext.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                	GotoMainPage();            	
                }
    		} );
    		
    	} else {
    		
    		mImageDevice.setImageDrawable(getResources().getDrawable(R.drawable.device_question));    
    		mTextDeviceName.setText(getResources().getString(R.string.unknow));    		
    		mImageNext.setImageDrawable(getResources().getDrawable(R.drawable.refresh));  
	
    		mImageNext.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                	ReConnect();            	
                }
    		} );

    	}
    }
    
    private boolean NmpDeviceOpen() {
    	
    	NmpDeviceClose();

		mNmpDevice = new CNmpDongle(this)
		{			
			public void DeviceStatus(int status){
				
				switch(status) {
					case CNmpDevice.DEVICE_STATUS_DISCONNECT:
						ConnectResultHandle(false);
						break;
					case CNmpDevice.DEVICE_STATUS_CONNECT:
						ConnectResultHandle(true);
						break;
				}				
			}
		};   
		
		if(!mNmpDevice.Connect())
		{
			ConnectResultHandle(false);				
		}		
		
    	return true;
    }
    
    private void NmpDeviceClose() {
    	
    	if(mNmpDevice != null) {    		
    		mNmpDevice.Disconnect();
    		mNmpDevice = null;
    	}    	
    }   
    
	@Override
	protected void onStop() 
	{
		// TODO Auto-generated method stub
		Log.d(this.toString(),"onStop start");		
		super.onStop();
		Log.d(this.toString(),"onStop end");
	}

	@Override
	protected void onDestroy() 
	{
		// TODO Auto-generated method stub
		Log.d(this.toString(),"onDestroy start");
		super.onDestroy();
		Log.d(this.toString(),"onDestroy end");
	}

	@Override
	protected void onPause() 
	{
		// TODO Auto-generated method stub
		Log.d(this.toString(),"onPause start");
		NmpDeviceClose();
		super.onPause();
		Log.d(this.toString(),"onPause end");
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) 
	{
		// TODO Auto-generated method stub
		Log.d(this.toString(),"onPostCreate start");
		super.onPostCreate(savedInstanceState);
		Log.d(this.toString(),"onPostCreate end");
	}

	@Override
	protected void onPostResume() 
	{
		// TODO Auto-generated method stub
		Log.d(this.toString(),"onPostResume start");
		super.onPostResume();
		Log.d(this.toString(),"onPostResume end");
	}

	@Override
	protected void onRestart() 
	{
		// TODO Auto-generated method stub
		Log.d(this.toString(),"onRestart start");
		super.onRestart();		
		Log.d(this.toString(),"onRestart end");
	}

	@Override
	protected void onResume() 
	{
		// TODO Auto-generated method stub
		Log.d(this.toString(),"onResume start");
		NmpDeviceOpen();
		super.onResume();
		Log.d(this.toString(),"onResume end");
	}

	@Override
	protected void onStart() 
	{
		// TODO Auto-generated method stub
		Log.d(this.toString(),"onStart start");
		super.onStart();
		Log.d(this.toString(),"onStart end");
	}

}